package com.xyyl.patient.common.easemob;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xyyl.patient.core.util.StringUtils;

/**
 * @projectName:xyyl-patient-web
 * @ClassName: EasemobRestClent
 * @Description:环信账号注册API
 * @author chens
 * @date 2017年8月22日
 *
 */
public class EasemobRestClent {

	/**
	 * 获得环信token
	 * 
	 * @return
	 */
	public static Map<String, String> getToken() {
		StringBuffer tokenURL = new StringBuffer();
		tokenURL.append(EasemobProperties.getProps("Uri"));
		tokenURL.append(EasemobProperties.getProps("org_name"));
		tokenURL.append("/");
		tokenURL.append(EasemobProperties.getProps("app_name"));
		tokenURL.append("/");
		tokenURL.append(EasemobUtil.PATHTYPE.token.name());
		JSONObject parameter = new JSONObject();
		parameter.put("grant_type", EasemobProperties.getProps("grant_type"));
		parameter.put("client_id", EasemobProperties.getProps("client_id"));
		parameter.put("client_secret", EasemobProperties.getProps("client_secret"));
		String tokenText = EasemobUtil.getToken(tokenURL.toString(), parameter.toString());
		JSONObject restJSON = JSON.parseObject(tokenText);
		Map<String, String> resMap = new HashMap<>();
		resMap.put("access_token", restJSON.getString("access_token"));
		resMap.put("expires_in", restJSON.getString("expires_in"));
		resMap.put("application", restJSON.getString("application"));
		return resMap;
	}

	/**
	 * 环信注册用户接口 支持多用户注册
	 * 
	 * @param parameter
	 *            [{ "username": "12212212222", "password": "123456" },{
	 *            "username": "12212212222", "password": "123456" }]
	 * @param token
	 * @return
	 */
	public static List<Map<String, String>> register(String parameter, String token) {
		List<Map<String, String>> userList = new ArrayList<>();
		StringBuffer regURL = new StringBuffer();
		regURL.append(EasemobProperties.getProps("Uri"));
		regURL.append(EasemobProperties.getProps("org_name"));
		regURL.append("/");
		regURL.append(EasemobProperties.getProps("app_name"));
		regURL.append("/");
		regURL.append(EasemobUtil.PATHTYPE.users.name());
		String resText = EasemobUtil.sendResult(parameter, token, regURL.toString(), EasemobUtil.REQUESTMETHOD.POST);
		if(StringUtils.isEmpty(resText)){
		   return userList;
		}
		JSONObject json = JSON.parseObject(resText);
		JSONArray array = JSON.parseArray(json.get("entities").toString());
		for (int i = 0; i < array.size(); i++) {
			JSONObject item = JSON.parseObject(array.get(i).toString());
			Map<String, String> user = new HashMap<>();
			user.put("uuid", item.getString("uuid"));
			user.put("type", item.getString("type"));
			user.put("created", item.getString("created"));
			user.put("modified", item.getString("modified"));
			user.put("username", item.getString("username"));
			user.put("activated", item.getString("activated"));
			userList.add(user);
		}
		return userList;
	}

	public static String getStatus(String parameter, String token) {
		StringBuffer regURL = new StringBuffer();
		regURL.append(EasemobProperties.getProps("Uri"));
		regURL.append(EasemobProperties.getProps("org_name"));
		regURL.append("/");
		regURL.append(EasemobProperties.getProps("app_name"));
		regURL.append("/");
		regURL.append(EasemobUtil.PATHTYPE.users.name());
		regURL.append("/");
		regURL.append(parameter);
		regURL.append("/");
		regURL.append("status");
		String resText = EasemobUtil.sendResult(null, token, regURL.toString(), EasemobUtil.REQUESTMETHOD.GET);
		JSONObject json = JSON.parseObject(resText);
		JSONObject status = json.getJSONObject("data");
		String res = status.getString(parameter);

		return res;

	}
}
